Skip to content

fix(auth): clear stale WebView2 cookies before GamePass re-login (#296)#297

Merged
YCC3741 merged 2 commits into
codefrom
fix/296-gamepass-relogin-stale-webview-cookies
Jun 2, 2026
Merged

fix(auth): clear stale WebView2 cookies before GamePass re-login (#296)#297
YCC3741 merged 2 commits into
codefrom
fix/296-gamepass-relogin-stale-webview-cookies

Conversation

@YCC3741

@YCC3741 YCC3741 commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #296 — after a GamePass login, logging out and logging in again (GamePass) would load the wrong / empty account data, and only fully closing and reopening Beanfun.exe recovered.

Root cause

WebView2 keeps a single cookie store per user-data-folder, shared by every window for the lifetime of the host process. Session cookies (bfWebToken, ASP.NET_SessionId) have no Expires, so their lifetime is tied to the WebView2 browser session = the whole process.

  • After a GamePass logout we clear AppState + the reqwest cookie jar and invalidate the server-side session, but the WebView2 cookie store is never touched.
  • The next GamePass login opens a new window that still shares the old cookies, so the portal sees the lingering (now dead) bfWebToken, short-circuits the OAuth round-trip, and the harvest lifts the invalidated session.
  • Restarting the .exe ends the WebView2 browser session, which is why it was the only recovery.

(The previous #287 only invalidated the prefetched account cache on game switch, which is unrelated to this WebView2 cookie persistence.)

Fix

Clear the WebView2 cookie store before seeding the fresh session cookies in open_gamepass_window, so every attempt starts from a clean, process-restart-equivalent state.

  • New native COM helper clear_all_cookies_native (DeleteAllCookies).
  • The clear and seed run as two separate native passes with a flush gap (clear → sleep → seed → sleep → navigate). DeleteAllCookies and AddOrUpdateCookie are both fire-and-return COM calls with no documented ordering guarantee, so fusing them into one pass risked the pending delete wiping the freshly-seeded cookies (which would reproduce the D5 "No such auth key and secret code" failure).
  • Windows-only path; non-Windows keeps the existing wry set_cookie seed (the quirk is WebView2-specific and the app ships Windows-only).
  • Added a per-page-load diagnostic (trace_webview_cookies) that logs the WebView's cookie names (never values) so the clear can be verified on a live run.

Test plan

  • cargo build (non-cached recompile), cargo clippy — no warnings
  • cargo test --lib gamepass — 22 passed
  • Manual: GamePass login → logout → GamePass login again → account data is correct without restarting the app
  • Manual: check step=GamepassPageLoad.WebViewCookies logs — the second login's entry page should show only freshly-seeded session cookies, no stale bfWebToken

Note: the native WebView2 cookie behaviour can't be exercised by automated unit tests (no WebView2 runtime in CI; cookie_native.rs has none by design), so the live verification above is required before merge.

YCC3741 added 2 commits May 21, 2026 22:32
WebView2 keeps a single cookie store per user-data-folder, shared by
every window for the lifetime of the host process. After a GamePass
logout the server-side session is invalidated but its bfWebToken /
ASP.NET_SessionId cookies linger in that store, so the next GamePass
login (a new window, same process) inherits the stale token, the
portal short-circuits the OAuth round-trip, and the harvest lifts the
dead session — surfacing wrong/empty account data. Only restarting the
.exe recovered, because that ends the WebView2 browser session.

Clear the WebView2 cookie store before seeding the fresh session
cookies so every attempt starts from a clean, process-restart-
equivalent state. The clear and seed run as two separate native COM
passes with a flush gap between them: DeleteAllCookies and
AddOrUpdateCookie are both fire-and-return calls with no documented
ordering guarantee, so fusing them into one pass risks the pending
delete wiping the freshly-seeded cookies.

Add a per-page-load diagnostic that logs the WebView's cookie names
(never values) so the clear can be verified on a live run.
@YCC3741 YCC3741 requested a review from lshw54 June 1, 2026 17:54
@YCC3741 YCC3741 self-assigned this Jun 1, 2026
@YCC3741 YCC3741 merged commit 9da134d into code Jun 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6.0.1 Bug回報

2 participants